<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/2/8
 * Time: 16:11
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\index\logic\NoteLogic;
use app\index\logic\UserLogic;

/**
 * 寺庙签列表
 * Class Temple
 * @package app\index\controller
 */
class Temple extends Frontend
{
    public function index(){
        $temple_id = $this->request->param('temple_id');
        $map['id'] = $temple_id;
        $temple = model('Temple')->where($map)->field('name,desc')->find();
        $this->view->assign('temple_name',$temple['name']);
        $this->view->assign('temple_desc',$temple['desc']);
        $this->view->assign('temple_id',$temple_id);
        return $this->view->fetch();
    }

    /**
     * ajax获取寺庙签列表
     * @return string
     */
    public function note_list(){
        $pagesize = 39;
        $pagenumber = $this->request->post('pagenum',1);
        $temple_id = $this->request->post('temple_id');
        $logic = new NoteLogic();
        $data = $logic->getNoteListByTemple($temple_id,$pagenumber,$pagesize);
        $this->view->assign('data', $data);
        $this->view->engine->layout(false);
        return $this->view->fetch('note_list');
    }

    /**
     * 绑定签
     * @return \think\response\Json
     */
    public function bind_note(){
        $user_id = $this->getUserId();
        $note_id = $this->request->param('note_id');
        $logic = new UserLogic();
        $result = $logic->bindNoteByUser($user_id,$note_id);
        $success = false;
        $msg = '';
        $rtnData = [];
        switch($result['status']){
            case 0:
                $rtnData = $result['error'];
                $msg = '绑定失败！';
                break;
            case 1:
                $success = true;
                break;
            case 2:
                $msg = $result['msg'];
                break;
        }
        if($success){
            return json(['success'=>$success,'msg'=>'绑定成功！','data'=>$result['data']]);
        }else{
            return json(['success'=>$success,'msg'=>$msg,'data'=>$rtnData]);
        }
    }

    /**
     * 跳转到寺庙详情
     * @return string
     */
    public function temple_detail(){
        $user_id = $this->getUserId();
        $user = model('User')->where(array('id'=>$user_id))->field('temple_id,avatar,nickname')->find();
        if(!empty($user['temple_id'])){
            $temple = $this->getTempleInfo($user['temple_id']);
            $this->view->assign('success',true);
            $this->view->assign('temple',$temple);
        }else{
            $temple['id'] = 0;
            $this->view->assign('temple',$temple);
            $this->view->assign('success',false);
        }
        $this->view->assign('user',$user);
        return $this->view->fetch();
    }

    private function getTempleInfo($temple_id){
        $temple_name = model('Temple')->where(array('id'=>$temple_id))->value('name');
        $note_ids_list = model('Note')
            ->where(array('temple_id'=>$temple_id))
            ->field('id')
            ->select();
        $note_ids = [];
        foreach ($note_ids_list as $k=>$v){
            $note_ids[]=$v['id'];
        }
        $solve_count = model('UserNote')
            ->where(array('is_solve'=>1))
            ->where('note_id','in',$note_ids)
            ->count();
        $begin_today=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $end_today=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $today_solve_count = model('UserNote')
            ->where(array('is_solve'=>1))
            ->where('note_id','in',$note_ids)
            ->whereTime('create_time', 'between', [$begin_today, $end_today])
            ->count();
        $qr_count = model('UserNote')
            ->where('note_id','in',$note_ids)
            ->count();
        $today_qr_count = model('UserNote')
            ->where('note_id','in',$note_ids)
            ->whereTime('create_time', 'between', [$begin_today, $end_today])
            ->count();
        $temple['solve_count'] = $solve_count;
        $temple['today_solve_count'] = $today_solve_count;
        $temple['qr_count'] = $qr_count;
        $temple['today_qr_count'] = $today_qr_count;
        $temple['name'] = $temple_name;
        $temple['id'] = $temple_id;
        return $temple;
    }

    /**
     * 获取解签数量
     * @return \think\response\Json
     */
    public function solve_count(){
        $start_time = $this->request->post('start_time');
        $end_time = $this->request->post('end_time');
        $temple_id = $this->request->post('temple_id');
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        $note_ids_list = model('Note')
            ->where(array('temple_id'=>$temple_id))
            ->field('id')
            ->select();
        $note_ids = [];
        foreach ($note_ids_list as $k=>$v){
            $note_ids[]=$v['id'];
        }
        $solve_count = model('UserNote')
            ->where(array('is_solve'=>1))
            ->where('note_id','in',$note_ids)
            ->whereTime('create_time', 'between', [$start_time, $end_time])
            ->count();

        $qr_count = model('UserNote')
            ->where('note_id','in',$note_ids)
            ->whereTime('create_time', 'between', [$start_time, $end_time])
            ->count();
        return json(['solve_count'=>$solve_count,'qr_count'=>$qr_count]);
    }

    public function test(){
        $user['id'] = 3;
        session('user',$user);
        var_dump(session('user'));
    }
}